<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="日常收入">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();

            ajaxSelects("${baseUrl}user/user/getAll.action.action",[
                {selectName:"userId",head:"全部" },
                {selectName:"makeUserId",head:"全部" }]);


            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId","全部");
            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId","全部");
            ajaxSelect("${baseUrl}budget/budgetProject/getAllIn.action", "budgetProjectId","全部");
        });
        layui.use(['form', 'table','laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;

            laydate.render({
                elem: '#date',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="date1"]').val(getDateFromLayUiDate(date));
                        $('[name="date2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="date1"]').val('');
                        $('[name="date2"]').val('');
                    }
                }
            });

            laydate.render({
                elem: '#makeTime',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="makeTime1"]').val(getDateFromLayUiDate(date));
                        $('[name="makeTime2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="makeTime1"]').val('');
                        $('[name="makeTime2"]').val('');
                    }
                }
            });

            $(".close").click(function () {
                closeAll();
            });

            renderTable();

            form.on('checkbox(noClose)', function (data) {
                table.reload('table',{
                    where: genSearch()
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{
                    where: genSearch()
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });

            table.on('toolbar(table)', function (obj) {
                if (obj.event === 'add') {
                    window.parent.openTableForSubIframe("新增支出","${baseUrl}budget/budgetInNew.action");
                } else if(obj.event === 'export') {
                    let data = table.checkStatus('table').data;
                    confirm("确认导出?", function () {
                        tipLoad("处理中...");
                        post("${baseUrl}budget/budgetIn/export.action" + genParam(genSearch()), {},function (resp) {
                            closeAll();
                            if (resp.code == 1) {
                                downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                            } else {
                                errTip(resp.msg);
                            }
                        });
                    });

                }
            });

            table.on('tool(table)', function (obj) {
                let data = obj.data;
                if (obj.event === 'close') {
                    closeBudget(data);
                } else if(obj.event == 'detail') {
                    window.parent.openTableForSubIframe("支出详情","${baseUrl}budget/budgetInDetail.action?id="+data.id);
                }
            });
        });





        function renderTable() {
            clearSearch();
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}budget/budget/getInList.action",
                where : {
                    close :false,
                    time:(new Date()).valueOf()
                },
                totalRow: true,
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                            return genTableIndex(d.close, "作废", d);
                        }},
                    {field: 'id', width: 10, title: 'ID',hide:true},
                    {field: 'no', width: 130, title: '单据编号',style:"font-size:12px;"},
                    {field: 'date', width: 100, title: '业务日期',style:"font-size:12px;",templet:function (d) {
                        return GMTToDate(d.date);
                    }},
                    {width: 100, title: '经手人',style:"font-size:12px;",templet: function (d) {
                            return d.user == null ? "" :d.user.name;
                        }},
                    {field: 'unit', width: 120, title: '付款单位',style:"font-size:12px;",templet:function (d) {
                            if (d.unitType == 1) {
                                return "";
                            } else if (d.unitType == 2) {
                                return d.custom.name;
                            } else if (d.unitType == 3){
                                return d.provider.name;
                            }
                    }},
                    {width: 100, title: '账目类型',style:"font-size:12px;",templet:function (d) {
                            return d.budgetProject !=undefined? d.budgetProject.name:"";
                    }},
                    {field: 'amt', width: 100, title: '金额',style:"font-size:12px;", templet:function (d) {
                            return getPrice(d.amt);
                        }},
                    {width: 120, title: '门店',style:"font-size:12px;",templet:function (d) {
                            return d.store != undefined ? d.store.name:"";
                        }},
                    {width: 120, title: '收入账户',style:"font-size:12px;",templet:function (d) {
                            return d.settAccountVo != undefined ? d.settAccountVo.name : "";
                        }},
                    {field: 'remark', minWidth: 60, title: '备注',style:"font-size:12px;"},
                    {title: '操作', minWidth: 80, fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){

                    let amt = 0;
                    $.each(res.data, function (i,d) {
                        if (d.close) {
                            return true;
                        }
                        amt += parseFloat(d.amt);
                    });

                    setTotalRow("amt",getPrice(amt));
                    setTotalRow("1","合计");

                    setTableLineCss("table",res.data, "close", true, "font-gray-c2c2c2");
                }
            });
        }

        function closeBudget(data) {
            layer.confirm('是否关闭单据《'+data.no+'》?', {icon: 3, title:'关闭单据'}, function(index){
                tipLoad("关闭中..");
                post("${baseUrl}budget/budget/close.action",{id:data.id}, function (resp) {
                    closeAll();
                    if (resp.code == 1){
                        tip("关闭成功");
                        renderTable();
                    } else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function clearSearch() {
            $("#searchForm")[0].reset();
            $('[name="date1"]').val('');
            $('[name="date2"]').val('');
            $('[name="makeTime1"]').val('');
            $('[name="makeTime2"]').val('');
        }

        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.close = $('[name="noClose"]').next().hasClass("layui-form-checked") ? false:'';
            return json;
        }
    </script>

</html:header>
<body style="background-color: #ffffff;overflow: auto;">
    <form class="layui-form layui-form-pane" action="" id="searchForm">
        <input type="hidden" name="date1">
        <input type="hidden" name="date2">
        <input type="hidden" name="makeTime1">
        <input type="hidden" name="makeTime2">
        <div class="layui-form-item">
            <div class="layui-inline">
                <input type="checkbox" name="noClose" title="不显示作废记录" lay-skin="primary" value="true" checked lay-filter="noClose">
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">其他信息</label>
                <div class="layui-input-inline">
                    <input type="text" id="search" name="search" placeholder="单号、单位、账目类型、备注" style="font-size: 12px;" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <a class="layui-btn" lay-submit="" lay-filter="search" id="btn-search">
                    <i class="layui-icon layui-icon-search"></i>
                </a>
                <a class="layui-btn" id="selectSearch">筛选</a>
            </div>


            <div class="table-tool-div-screen" status="close" style="display: none;">
                <div class="layui-inline">
                    <label class="layui-form-label">制单日期</label>
                    <div class="layui-input-inline">
                        <input type="text" id="makeTime" placeholder="请选择范围" class="layui-input" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">业务日期</label>
                    <div class="layui-input-inline">
                        <input type="text" id="date" placeholder="请选择范围" class="layui-input" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">制单人</label>
                    <div class="layui-input-inline">
                        <select name="makeUserId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">门店</label>
                    <div class="layui-input-inline">
                        <select name="storeId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">账目类型</label>
                    <div class="layui-input-inline">
                        <select name="budgetProjectId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">付款单位</label>
                    <div class="layui-input-inline">
                        <input type="text" name="unit" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">账户</label>
                    <div class="layui-input-inline">
                        <select name="settAccountId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">经办人</label>
                    <div class="layui-input-inline">
                        <select name="userId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <input type="text" name="remark" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                    <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                </div>
                <script>
                    $("#selectSearch").click(function () {
                        if ($(".table-tool-div-screen").attr("status")=="close") {
                            $(".table-tool-div-screen").css("display","block");
                            $(".table-tool-div-screen").attr("status","open");
                            $("#search").parent().parent().css("display","none");
                            $("#search").val("");
                            $("#btn-search").css("display","none");
                            $(this).html("收起");
                        } else {
                            $(".table-tool-div-screen").css("display","none");
                            $(".table-tool-div-screen").attr("status","close");
                            $("#search").parent().parent().css("display","inline-block");
                            $("#btn-search").css("display","inline-block");
                            $(this).html("筛选");
                        }
                    });

                    $("#clearSearch").click(function () {
                        clearSearch();
                    });
                </script>
            </div>
        </div>
    </form>
    <hr/>

    <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

    <script type="text/html" id="tableToolBar">
        <div class="layui-inline">
            <a class="layui-btn" lay-event="add">新增</a>
        </div>

        <div class="layui-inline">
            <a class="layui-btn layui-btn-primary" lay-event="export">导出</a>
        </div>
    </script>

    <script type="text/html" id="operBar">
        <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>
        {{#  if((_login_user.cancelOtherOrder && !d.close) || _login_user.id==d.userId){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="close">作废</a>
        {{#  }  }}
    </script>
</body>
<html:loading/>
</html>